package com.GLM.Viewer.Users;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.GLM.Viewer.R;
import com.GLM.Viewer.MyNews.ViewPagerAdapter;

import dalvik.system.VMRuntime;

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class UserSettingTutorialsActivity extends Activity implements OnPageChangeListener,OnTouchListener{
	private int[] mItemImages = new int[]{R.drawable.tutorials1, R.drawable.tutorials2, R.drawable.tutorials3,
				R.drawable.tutorials4, R.drawable.tutorials5, R.drawable.tutorials6, R.drawable.tutorials7};
	private List<View> listViews;
	private ViewPager viewPager;
	private ViewPagerAdapter viewPagerAdapter;
	private int screenWidth;
	private int screenHeight;
	private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_userdetail_tutorials);
		VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);
		screenHeight = getWindowManager().getDefaultDisplay().getHeight();
		screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		listViews = new ArrayList<View>();
		
		initListViews();
		listViews.add(0, new View(this));
		listViews.add(listViews.size(), new View(this));
		initViewPager();
	}

	public void initListViews(){
		for(int i=0; i<mItemImages.length; i++){
			ViewGroup vg = getViewsBackgroundView(mItemImages[i]);
			listViews.add(vg);
		}
	}
	
	private ViewGroup getViewsBackgroundView(int resourceIndex) {
		FrameLayout layout = new FrameLayout(this);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

		ImageView backImage = new ImageView(this);
		FrameLayout.LayoutParams backLp = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.CENTER);
		backImage.setLayoutParams(backLp);
		backImage.setImageBitmap(decodeSampledBitmapFromResource(getResources(), resourceIndex, screenWidth,screenHeight));
		layout.addView(backImage);

		return layout;
	}

	private void initViewPager() {
		viewPagerAdapter = new ViewPagerAdapter(listViews);
		viewPager = (ViewPager) findViewById(R.id.tutorials_viewpager);
		viewPager.setAdapter(viewPagerAdapter);
		viewPager.setCurrentItem(1);
		viewPager.setOnPageChangeListener(this);
	}


	public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {

		final BitmapFactory.Options options = new BitmapFactory.Options();
		InputStream is = getResources().openRawResource(resId);

        options.inSampleSize = 2;

        options.inJustDecodeBounds = false;
        Bitmap bmp = BitmapFactory.decodeStream(is, null, options);
       
        return bmp;
	}

	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return false;
	}



	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}



	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}

	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		if (position == 0) {
			viewPager.setCurrentItem(position + 1);
		} else if (position == listViews.size() - 1) {
			viewPager.setCurrentItem(position - 1);
		}
	}

	

	
}
